﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using DaoGenerator;
using LogicGenerator;
using NHibernateGenerator;
using SupportGenerator;
using WebViewGenerator;

namespace GenerationSelector
{
	public partial class MainSelector : Form
	{
		private Assembly assemblyInfo;


		public MainSelector()
		{
			this.InitializeComponent();
		}


		private void selectDllButton_Click(
			object sender,
			EventArgs e)
		{
			this.dllSelectorDialogBox.ShowDialog();
		}


		private void dllSelectorDialogBox_FileOk(
			object sender,
			CancelEventArgs e)
		{
			LoadTypes();
		}

		private void LoadTypes()
		{
			this.selectedDllTextBox.Text = this.dllSelectorDialogBox.FileName;

			this.assemblyInfo = Assembly.LoadFrom(this.selectedDllTextBox.Text);

			Type[] types = this.assemblyInfo.GetTypes();

			var typenames = from type in types
							orderby type.FullName
							where type.Namespace.StartsWith(Namespace.Text)
							select type.FullName;

			this.classNameListingListBox.DataSource = typenames.ToList();
		}


		private void generateButton_Click(
			object sender,
			EventArgs e)
		{
			IEnumerable<string> selectedItems = this.classNameListingListBox.SelectedItems.Cast<string>();

			if (this.nhibernateMappingFilesCheckBox.Checked) {
				new NHibernateMappingFileGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.nhibernateDaoCheckBox.Checked) {
				new NHibernateDaoFileGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.daoInterfaceCheckBox.Checked) {
				new DaoInterfaceFileGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.filterFileCheckBox.Checked) {
				new FilterFileGenerator(this.assemblyInfo).Execute(selectedItems);
				new FilterFillFileGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.parameterNamesCheckBox.Checked) {
				new ParameterNamesGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.itemNamesCheckBox.Checked) {
				new ItemNamesGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.deserializeCheckBox.Checked) {
				new DeserializeGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.getActionCheckBox.Checked) {
				new GetActionFileGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.saveActionCheckBox.Checked) {
				new SaveActionFileGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.createActionCheckBox.Checked) {
				new CreateFromRequestFileGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.populateActionCheckBox.Checked) {
				new PopulateFromRequestFileGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if (this.deleteActionCheckBox.Checked) {
				new DeleteActionFileGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if(this.statesConfigCheckBox.Checked) {
				new StatesConfigFileGenerator(this.assemblyInfo).Execute(selectedItems, int.Parse(startStateTextBox.Text));
			}

			if(this.rolesCheckBox.Checked) {
				new RolesCsvFileGenerator(this.assemblyInfo).Execute(selectedItems, int.Parse(startStateTextBox.Text));
			}

			if(this.validatorCheckBox.Checked) {
				new ValidatorFileGenerator(this.assemblyInfo).Execute(selectedItems, int.Parse(startStateTextBox.Text));
			}

			if(this.pagesCheckBox.Checked) {
				new PagesFileGenerator(this.assemblyInfo).Execute(selectedItems);
			}

			if(this.nhibernateConfigInsertCheckBox.Checked) {
				new NHibernateConfigInsertGenerator(this.assemblyInfo).Execute(selectedItems);
			}

		}

		private void Namespace_TextChanged(object sender, EventArgs e)
		{
			LoadTypes();
		}

		private void openOutputButton_Click(object sender, EventArgs e)
		{
			Process.Start(AppDomain.CurrentDomain.BaseDirectory + @"\GeneratedFiles\");
		}

		private void selectAllButton_Click(object sender, EventArgs e)
		{
			this.nhibernateMappingFilesCheckBox.Checked = true;
			this.pagesCheckBox.Checked = true;
			this.validatorCheckBox.Checked = true;
			this.rolesCheckBox.Checked = true;
			this.statesConfigCheckBox.Checked = true;
			this.deleteActionCheckBox.Checked = true;
			this.populateActionCheckBox.Checked = true;
			this.createActionCheckBox.Checked = true;
			this.saveActionCheckBox.Checked = true;
			this.getActionCheckBox.Checked = true;
			this.deserializeCheckBox.Checked = true;
			this.itemNamesCheckBox.Checked = true;
			this.parameterNamesCheckBox.Checked = true;
			this.filterFileCheckBox.Checked = true;
			this.daoInterfaceCheckBox.Checked = true;
			this.nhibernateDaoCheckBox.Checked = true;
			this.nhibernateMappingFilesCheckBox.Checked = true;
			this.nhibernateConfigInsertCheckBox.Checked = true;
		}

		private void clearAllButton_Click(object sender, EventArgs e)
		{
			this.nhibernateMappingFilesCheckBox.Checked = false;
			this.pagesCheckBox.Checked = false;
			this.validatorCheckBox.Checked = false;
			this.rolesCheckBox.Checked = false;
			this.statesConfigCheckBox.Checked = false;
			this.deleteActionCheckBox.Checked = false;
			this.populateActionCheckBox.Checked = false;
			this.createActionCheckBox.Checked = false;
			this.saveActionCheckBox.Checked = false;
			this.getActionCheckBox.Checked = false;
			this.deserializeCheckBox.Checked = false;
			this.itemNamesCheckBox.Checked = false;
			this.parameterNamesCheckBox.Checked = false;
			this.filterFileCheckBox.Checked = false;
			this.daoInterfaceCheckBox.Checked = false;
			this.nhibernateDaoCheckBox.Checked = false;
			this.nhibernateMappingFilesCheckBox.Checked = false;
			this.nhibernateConfigInsertCheckBox.Checked = false;
		}
	}
}